//
//  YMHomeViewController.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/19.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

private let HomeCellId = "home"

class YMHomeViewController: YMBaseViewController {
    
    var listViewModel:YMListViewModel = YMListViewModel()

      var dataArr = [[YMStatus]]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

         if !isLogin{
            
            visitorView?.setVisitorView(nil, imgName: nil)
            return
         }
        
        getHomeData()

        setupTableViewInfo()
        
    }

    
    func setupTableViewInfo() {

        tableView.dataSource = self
        tableView.delegate = self
        tableView.registerClass(YMStatusCell.self, forCellReuseIdentifier: HomeCellId)
        
        tableView.estimatedRowHeight = 200
        tableView.tableFooterView = indicatorView
        tableView.addSubview(refreshControl)
        navigationController?.navigationBar.insertSubview(pullDownTipLabel, atIndex: 0)
        
        refreshControl.addTarget(self, action: #selector(YMHomeViewController.refreshActionGo), forControlEvents: .ValueChanged)
    }
    
    func refreshActionGo() {
        
        getHomeData()
    }
    //懒加载控件
    //菊花
    private lazy var indicatorView:UIActivityIndicatorView = {
       let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        view.color = ThemeColor
        return view
    }()
    //下拉刷新控件
    private lazy var refreshControl: YMPullDownRefresh = YMPullDownRefresh()
    
    //下拉提醒框
    
    private lazy var pullDownTipLabel:UILabel = {
       let lab = UILabel(frame:CGRect(x: 0, y: 64 - 30 - 30, width: SCREENW, height: 30 ))
        lab.textColor = UIColor.whiteColor()
        lab.font = UIFont.systemFontOfSize(12)
        lab.backgroundColor = ThemeColor
        lab.textAlignment = .Center
        lab.hidden = true
        return lab
    }()
    
}

//        MARK: - tableView设置
extension YMHomeViewController:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return listViewModel.statusList.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! YMStatusCell
        
        cell.statusViewModel = listViewModel.statusList[indexPath.row]
        

        return cell
    }
    
//            MARK: - 即将显示的cell的调用方法
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if listViewModel.statusList.count - 1 == indexPath.row && !indicatorView.isAnimating(){
            indicatorView.startAnimating()
            getHomeData()
        }
    }
    
}


//        MARK: - 从listViewmodel获取微博数据
extension YMHomeViewController{
    
    func getHomeData() {

        listViewModel.loadHomeData(indicatorView.isAnimating()) { (isSuc, count) in
           
            self.endRefreshing()
            //判断书否成功,成功刷新就好
            if !isSuc {
                
                return
            }
            self.tableView.reloadData()
            
            if count >= 0{
            self.setPullDownTipLabelAnim(count)
            }
        }

    }
    
    func endRefreshing() {
        //停止动画
        self.indicatorView.stopAnimating()
        self.refreshControl.endRefreshing()
    }
    
    
    //        MARK: - 设置刷新提醒
    func setPullDownTipLabelAnim(count:Int) {
        
        if pullDownTipLabel.hidden == false{
            
            return
        }
        
        
        pullDownTipLabel.hidden = false
        
        var name = ""
        
        if count == 0 {
            name = "已经是最新的微博了"
        }else{
            
            name = "更新了\(count)条微博"
        }
        
        pullDownTipLabel.text = name
        
        UIView.animateWithDuration(0.5, animations: { 
            
            self.pullDownTipLabel.transform = CGAffineTransformMakeTranslation(0, 40)
            
            }) { (_) in
              
                UIView.animateWithDuration(0.5, delay: 1, options: [], animations: {
                    self.pullDownTipLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) in
                        self.pullDownTipLabel.hidden = true
                })
                
                
        }
        
    }

}




















